package listener;

import javax.servlet.http.HttpSession;
import javax.servlet.http.HttpSessionEvent;
import javax.servlet.http.HttpSessionListener;

/**
 * 实现了 HttpSessionListener 接口的类
 * 就是一个监听Session创建或销毁的监听器
 * 可以用于监控用户上线，离线
 */
public class LmyHttpSessionListener implements HttpSessionListener {

    @Override
    public void sessionCreated(HttpSessionEvent se) {
        HttpSession session = se.getSession();
        System.out.println("LmyHttpSessionListener 监听到session创建 = " +
                session.getId());

        //给session设置生命周期 30s
        session.setMaxInactiveInterval(30);
    }

    @Override
    public void sessionDestroyed(HttpSessionEvent se) {
        HttpSession session = se.getSession();
        System.out.println("LmyHttpSessionListener 监听到session被销毁 = " +
                session.getId());
    }
}
